package com.sky.controller.admin;

import com.sky.constant.RedisConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealDishService;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author : xiongke
 * @Description:
 * @Date: 2024/8/28
 */
@RestController
@Api(tags = "套餐管理相关接口")
@RequestMapping("/admin/setmeal")
@Slf4j
public class SetmealController {

    @Resource
    SetmealService setmealService;
    @Resource
    SetmealDishService setmealDishService;

    @ApiOperation("新增套餐")
    @PostMapping
    @CacheEvict(value = RedisConstant.DISH_CACHE , allEntries = true)
    public Result save(@RequestBody SetmealDTO dto) {
        log.info("新增套餐");
        setmealService.save(dto);
        return Result.success();
    }

    @ApiOperation("套餐分页查询")
    @GetMapping("/page")
    public Result<PageResult> queryPage(SetmealPageQueryDTO dto) {
        log.info("套餐分页查询：{}", dto);
        PageResult result = setmealService.queryPage(dto);
        return Result.success(result);
    }

    @ApiOperation("起售停售套餐")
    @PostMapping("/status/{status}")
    @CacheEvict(value = RedisConstant.DISH_CACHE , allEntries = true)
    public Result startOrStop(@PathVariable Integer status, Long id) {
        log.info("更改套餐售卖状态");
        setmealService.startOrStop(status, id);
        return Result.success();
    }

    @ApiOperation("批量删除套餐")
    @DeleteMapping
    @CacheEvict(value = RedisConstant.DISH_CACHE , allEntries = true)
    public Result deleteBatch(@RequestParam List<Long> ids) {
        log.info("删除套餐：{}", ids);
        setmealService.deleteBatch(ids);
        return Result.success();
    }

    @ApiOperation("根据id查询套餐")
    @GetMapping("/{id}")
    public Result<SetmealVO> getById(@PathVariable Long id){
        log.info("根据id:{}查询套餐",id);
        SetmealVO setmealVO = setmealService.getSetmealById(id);
        return Result.success(setmealVO);
    }

    @ApiOperation("修改套餐")
    @PutMapping
    @CacheEvict(value = RedisConstant.DISH_CACHE , allEntries = true)
    public Result updateSetmeal(@RequestBody SetmealDTO dto){
        log.info("修改套餐信息：{}",dto);
        setmealService.updateByCondition(dto);
        return Result.success();
    }
}
